package com.sc.controller;

import com.github.pagehelper.PageInfo;
import com.sc.pojo.Information;
import com.sc.pojo.User;
import com.sc.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

/**
 * @author 陈远坤
 */
@Controller
@RequestMapping("/information")
public class InfromationController {
    @Autowired
    InformationService is;
    //发送信息  回复信息
    @RequestMapping("/addInformation")
    public String addInformation(String title, String content, HttpSession session,Integer accepts,String type){
        User user =(User) session.getAttribute("theUser");
        System.out.println(accepts);
        Information ins = new Information(user.getId(),accepts,title,content,1,1,new Date());
        is.addInformation(ins);
        if ("1".equals(type)) return "redirect:/information/selectAllInfs";
        return "redirect:/friends/selectAllfs";
    }
    //查询所有的信息
    @RequestMapping("/selectAllInfs")
    public String selectAllInfs(HttpSession session, @RequestParam(defaultValue = "1") Integer pageNum, Map m, String likes){
        User user =(User) session.getAttribute("theUser");
        if (likes==null){
            PageInfo<Information> p = is.selectAllInfs(user.getId(),pageNum,4);
            m.put("p",p);
        }else {
            PageInfo<Information> p = is.selectAllInfsByLikes(user.getId(),pageNum,4,likes);
            m.put("p",p);
            m.put("likes",likes);
        }
        return "forward:/information/toInformation";
    }
    //查看发件箱
    @RequestMapping("/selectAllInfsByMy")
    public String selectAllInfsByMy(HttpSession session,@RequestParam(defaultValue = "1")Integer pageNum,Map map,String likes){
        User user =(User) session.getAttribute("theUser");
        if (likes==null){
            PageInfo<Information> p = is.selectAllInfsByMy(user.getId(),pageNum,4);
            map.put("p",p);
        }else {
            PageInfo<Information> p = is.selectAllInfsByMyByLikes(user.getId(),pageNum,4,likes);
            map.put("p",p);
            map.put("likes",likes);
        }
        return "forward:/information/toInformationfjx";
    }
    //把信息丢进垃圾箱
    @RequestMapping("/updateAcceptState")
    public String updateAcceptState(Integer id){
        is.updateAcceptState(id);
        return "redirect:/information/selectAllInfs";
    }
    //全部丢进垃圾箱
    @RequestMapping("/updateAllAcceptState")
    public String updateAllAcceptState(Integer[] cks){
        if (cks==null)  return "forward:/information/selectAllInfs";
        for (int i = 0; i < cks.length; i++) {
            is.updateAcceptState(cks[i]);
        }
        return "redirect:/information/selectAllInfs";
    }
    //彻底删除
    @RequestMapping("/deleteInfs")
    public String deleteInfs(Integer id,String type){
        is.deleteById(id);
        if ("1".equals(type)){
            return "redirect:/information/selectLjx";
        }
        return "redirect:/information/selectAllInfs";
    }
    //全部彻底删除
    @RequestMapping("/deleteAllInfs")
    public String deleteAllInfs(Integer[] cks,String type){
        if (cks==null && "2".equals(type))  return "forward:/information/selectLjx";
        if (cks==null && "1".equals(type))  return "forward:/information/selectAllInfsByMy";
        if (cks==null)  return "forward:/information/selectAllInfs";
        for (int i = 0; i < cks.length; i++) {
            is.deleteById(cks[i]);
        }

        if ("2".equals(type)) return "redirect:/information/selectLjx";
        if ("1".equals(type)) return "redirect:/information/selectAllInfsByMy";
        return "redirect:/information/selectAllInfs";
    }
    //发件箱删除
    @RequestMapping("/updateSend")
    public String updateSend(Integer id){
        is.updateSend(id);
        return "redirect:/information/selectAllInfsByMy";
    }
    //查看垃圾箱
    @RequestMapping("/selectLjx")
    public String selectLjx(@RequestParam(defaultValue = "1") Integer pageNum,Map m,String likes,HttpSession session){
        User user =(User) session.getAttribute("theUser");
        if (likes==null){
            PageInfo<Information> p = is.selectAllLjInfos(user.getId(),pageNum,4);
            m.put("p",p);
        }else {
            PageInfo<Information> p = is.selectAllLjInfosByLikes(user.getId(),pageNum,4,likes);
            m.put("p",p);
            m.put("likes",likes);
        }
        return "forward:/information/toInformatonLjx";
    }
    //已读
    @RequestMapping("/updateYidu")
    @ResponseBody
    public void updateYidu(Integer id){
        is.updateAcceptStateByYidu(id);
    }
    //垃圾箱恢复信息
    @RequestMapping("/updateAcceptStateinLjx")
    public String updateAcceptStateinLjx(Integer id){
        is.updateAcceptStateinLjx(id);
        return "redirect:/information/selectLjx";
    }
    //批量恢复到收件箱
    @RequestMapping("/updateAllAcceptStateinLjx")
    public String updateAllAcceptStateinLjx(Integer[] cks){
        if (cks==null) return "redirect:/information/selectLjx";
        for (int i = 0; i < cks.length; i++) {
            is.updateAcceptStateByYidu(cks[i]);
        }
        return "redirect:/information/selectLjx";
    }
    //去收件箱
    @RequestMapping("/toInformation")
    public String toInformation(){
        return "/information/informationList";
    }
    //去发件箱
    @RequestMapping("/toInformationfjx")
    public String toInformationfjx(){
        return "/information/informationfjx";
    }
    //去垃圾箱
    @RequestMapping("/toInformatonLjx")
    public String toInformatonLjx(){
        return "/information/ljInformation";
    }
}
